package com.piece.core.verifycode.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.piece.core.framework.constant.ApplicationConstants;
import com.piece.core.framework.properties.ApplicationProperties;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.verifycode.properties.VerifyCodeProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import java.util.Properties;

/**
 * 验证码配置
 */
public class VerifyCodeAutoConfig {

    @Autowired
    private ApplicationProperties applicationProperties;

    @Autowired
    private VerifyCodeProperties verifyCodeProperties;

    @Bean(name = "defaultCaptcha")
    public DefaultKaptcha getCaptchaBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        generateDefaultProperties(properties, 0);
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    @Bean(name = "defaultMathCaptcha")
    public DefaultKaptcha getMathCaptchaBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        generateDefaultProperties(properties, 0);
        // 验证码文本生成器
        properties.setProperty("kaptcha.textproducer.impl", verifyCodeProperties.getTextproducerImpl());
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    public void generateDefaultProperties(Properties properties, int length) {
        // 是否有边框 默认为true 我们可以自己设置yes，no
        properties.setProperty("kaptcha.border", verifyCodeProperties.getBorder());
        // 边框颜色 默认为Color.BLACK
        properties.setProperty("kaptcha.border.color", verifyCodeProperties.getBorderColor());
        // 验证码文本字符颜色 默认为Color.BLACK
        properties.setProperty("kaptcha.textproducer.font.color", verifyCodeProperties.getFontColor());
        // 验证码图片宽度 默认为200
        properties.setProperty("kaptcha.image.width", Convert.toStr(applicationProperties.getValue(ApplicationConstants.KAPTCHA_IMG_WIDTH), "200"));
        // 验证码图片高度 默认为50
        properties.setProperty("kaptcha.image.height", Convert.toStr(applicationProperties.getValue(ApplicationConstants.KAPTCHA_IMG_HEIGHT), "50"));
        // 图片样式
        properties.setProperty("kaptcha.obscurificator.impl", verifyCodeProperties.getObscurificatorImpl());
        // 干扰实现类
        if (StringUtil.isNotEmpty(verifyCodeProperties.getNoiseImpl())) {
            properties.setProperty("kaptcha.noise.impl", verifyCodeProperties.getNoiseImpl());
        }
        // 验证码文本字符大小 默认为40
        properties.setProperty("kaptcha.textproducer.font.size", verifyCodeProperties.getFontSize());
        // 验证码文本字符间距 默认为2
        properties.setProperty("kaptcha.textproducer.char.space", verifyCodeProperties.getCharSpace());
        // 验证码文本字符长度 默认为4
        if (length > 0) {
            properties.setProperty("kaptcha.textproducer.char.length", length + "");
        } else {
            properties.setProperty("kaptcha.textproducer.char.length", verifyCodeProperties.getCharLength());
        }
        // 验证码文本字体样式
        properties.setProperty("kaptcha.textproducer.font.names", verifyCodeProperties.getFontNames());
        // 验证码噪点颜色 默认为Color.BLACK
        properties.setProperty("kaptcha.noise.color", verifyCodeProperties.getNoiseColor());
    }
}
